package com.ziqing.apeletnews.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.ziqing.apeletnews.R;
import com.ziqing.apeletnews.activity.MessageDetailsActivity;
import com.ziqing.apeletnews.adapter.RemindAdapter;
import com.ziqing.apeletnews.base.BaseAdapter;
import com.ziqing.apeletnews.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("ValidFragment")
public class RemindFragment extends BaseFragment {
    @BindView(R.id.Rv_remind)
    RecyclerView Rv_remind;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout mTkRefresh;
    private String title;
    private RemindAdapter adapter;
    private List<String> datas=new ArrayList<>();
    public RemindFragment(String title) {
        this.title = title;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_remind, container, false);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    private void initData() {
        datas.add("http://edu-image.nosdn.127.net/79579C05F01E104654F1DF15B57E540D.jpg?imageView&thumbnail=286y170&quality=100");
        datas.add("http://edu-image.nosdn.127.net/368b3b46-7879-4e1f-9907-6250f413fafe.jpg?imageView&thumbnail=286y170&quality=100");
        datas.add("http://edu-image.nosdn.127.net/18BAE157B9454A43FF5F449A95D50DB1.png?imageView&thumbnail=286y170&quality=100");
        datas.add("http://edu-image.nosdn.127.net/368b3b46-7879-4e1f-9907-6250f413fafe.jpg?imageView&thumbnail=286y170&quality=100");
        datas.add("http://edu-image.nosdn.127.net/86E308D7548CE6293F95889B2A7DADC1.jpg?imageView&thumbnail=286y170&quality=100");
        datas.add("http://edu-image.nosdn.127.net/56A69935233D86C0F606F95144D940EF.png?imageView&thumbnail=225y125&quality=100");
    }

    @Override
    protected void initView() {
        ProgressLayout headView = new ProgressLayout(getContext());
        headView.setColorSchemeColors(ContextCompat.getColor(getContext(), R.color.colorAccent), ContextCompat.getColor(getContext(), R.color.Yellow),
                ContextCompat.getColor(getContext(), R.color.Green));
        mTkRefresh.setHeaderView(headView);

        mTkRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                new Handler().postDelayed(() -> refreshLayout.finishRefreshing(),2000);
                super.onRefresh(refreshLayout);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);

            }
        });
        Rv_remind.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter=new RemindAdapter(getContext(),datas,R.layout.layout_remind_item);
        Rv_remind.setAdapter(adapter);
        adapter.setOnItemClickListener(position -> {
         startActivity(new Intent(getContext(), MessageDetailsActivity.class).putExtra("ICON",adapter.getListData().get(position).toString()));
        });
    }
}